#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# TÜBİTAK/UEAKE
# Gökmen GÖKSEL (2008) <gokmen@pardus.org.tr>

import os
import ConfigParser
import Developer
import Exceptions

default_attr = ["realname","email","homedir","svndir","svnrepos"]

class Pet:
    """ Pet Main Class """
    def __init__(self, username=''):
        self.config_path = os.path.join(os.getenv("HOME"),".pet/config.pet")

        if not os.path.exists(self.config_path):
            if username == '':
                raise Exceptions.NotFound, "No user defined. Define a new user or use proper config file."
            self.create_newconfig(username)

        # Let create our developer
        self.developer = Developer.Developer(username, self.config_path)

    def create_newconfig(self, username):
        confdir = os.path.dirname(self.config_path)
        if not os.path.exists(confdir):
            os.mkdir(confdir)
        config = file(self.config_path, 'w')
        parser = ConfigParser.ConfigParser()
        parser.add_section("developer")
        parser.set("developer", "username", username)
        for defaults in default_attr:
            parser.set("developer", defaults, '')
        parser.set("developer", "repos", '')
        parser.write(config)
        print "Config file has been created."
        config.close()

    def updatesvn(self,repo=''):
        if not repo == '':
            print "Now updating : %s .." % repo
            os.system("svn update %s " % os.path.join(self.developer.svndir,repo))
        else:
            for repo in self.developer.svnrepos.split(','):
                print "Now updating : %s .." % repo
                os.system("svn update %s " % os.path.join(self.developer.svndir,repo))
